package com.pet.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.entity.PetAnimal;
import com.pet.entity.PetApplcation;
import com.pet.service.PetAnimalService;
import com.pet.service.PetApplcationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author cxq
 *
 *
 * @date 2023/11/14
 */
@RestController
@RequestMapping("petApplcation")
@Api(value = "提供数据显示所需的接口",tags = "领养申请管理")
public class PetApplcationController extends ApiController {

    @Resource
    private PetApplcationService petApplcationService;

    @Resource
    private PetAnimalService petAnimalService;

    /**
     * 分页查询所有数据
     *
     * @param page      分页对象
     * @param petApplcation 查询实体
     * @return 所有数据
     */
    @ApiOperation("分页接口")
    @GetMapping
    public R selectAll(Page<PetApplcation> page, PetApplcation petApplcation) {
        return success(this.petApplcationService.page(page, new QueryWrapper<>(petApplcation)));
    }
//    @GetMapping("/page")
//    public R findPage(@RequestParam(defaultValue = "") String name,
//                           @RequestParam Integer pageNum,
//                           @RequestParam Integer pageSize) {
//        List<PetAnimal> animals = petAnimalService.list();
//        QueryWrapper<PetApplcation> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
//        if (!"".equals(name)) {
//            queryWrapper.like("name", name);
//        }
//        Page<PetApplcation> page = petApplcationService.page(new Page<>(pageNum, pageSize), queryWrapper);
//        for (PetApplcation record : page.getRecords()) {
//            animals.stream().filter(animal -> animal.getAnimal_id().equals(record.getAnimal_id())).findFirst().ifPresent(record::setAnimal);
//        }
//        return success(page);
//    }

    /**
     * 新增数据
     *
     * @param petApplcation 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("插入数据")
    public R insert(@RequestBody PetApplcation petApplcation) {
        return success(this.petApplcationService.save(petApplcation));
    }

    /**
     * 修改数据
     *
     * @param petApplcation 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody PetApplcation petApplcation) {
        return success(this.petApplcationService.updateById(petApplcation));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.petApplcationService.removeByIds(idList));
    }

    /**
     * 修改审核状态
     * @param id
     * @param state
     * @return
     */
    @ApiOperation("修改审核状态")
    @PostMapping("/state/{id}/{state}")
    public R state(@PathVariable Integer id, @PathVariable String state) {
        PetApplcation applcation = petApplcationService.getById(id);
        applcation.setAp_state(state);

        QueryWrapper<PetApplcation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("animal_id", applcation.getAnimal_id());
        List<PetApplcation> list = petApplcationService.list(queryWrapper);
        for (PetApplcation app : list) {
            app.setAp_state("审核不通过");
            petApplcationService.updateById(app);
        }

        petApplcationService.updateById(applcation);

        PetAnimal animal = petAnimalService.getById(applcation.getAnimal_id());
        animal.setAnimal_is_adopt("是");
        animal.setAnimal_adopt("不可领养");
        return success(this.petAnimalService.updateById(animal));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("通过id查询数据")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.petApplcationService.getById(id));
    }


}
